package com.inossem.warehousestorage_inossem.request.base;

import android.content.Context;

import com.google.gson.Gson;
import com.inossem.utils.base.Utils;
import com.inossem.warehousestorage_inossem.bean.base.CheckVersionBean;
import com.inossem.warehousestorage_inossem.constant.RequestConstant;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.listener.StringRequestListener;
import com.inossem.warehousestorage_inossem.manage.RequestManage;
import com.inossem.warehousestorage_inossem.model.StringResponse;
import com.inossem.warehousestorage_inossem.request.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by leij on 2017/6/13.
 */

public class CheckVersionRequest extends Request {

    private static final String VERSION_CODE = "versionCode";//APP的versionCode

    public static String request(Context context, GsonBeanListener gsonBeanListener) {
        String tag;
        Map<String, String> params = new HashMap<>();
        params.put(VERSION_CODE, String.valueOf(Utils.getVersionCode(context)));
        String url = RequestManage.getUrl(context, RequestConstant.URL_GET_VERSION + RequestManage.mapEncodeToParams(params));
        tag = RequestManage.getHttpsGetWithLoading(context, url, new StringRequestListener() {
            @Override
            public void onSuccess(StringResponse stringRequestResponse) {
                Gson gson = new Gson();
                CheckVersionBean bean = gson.fromJson(stringRequestResponse.getResponseInfo(), CheckVersionBean.class);
                excuteJsonBeanNew(context, bean, gsonBeanListener);
            }

            @Override
            public void onDefaultError(Context context, StringResponse stringRequestResponse) {
                super.onDefaultError(context, stringRequestResponse);
//                LogUtils.i("CheckVersionRequest");
                gsonBeanListener.onDefaultError(context, stringRequestResponse.getResponseCode(), stringRequestResponse.getResponseInfo());
            }

        });
        return tag;
    }

}
